[svn] / public / sloppy / example.lsl Repository:
ViewVC logotype

View of /public/sloppy/example.lsl

Parent Directory Parent Directory | Revision Log Revision Log

Revision 42 - (download) (annotate)
Sun Aug 31 23:29:30 2008 UTC (28 hours, 21 minutes ago) by anari
File size: 6416 byte(s)
Minor fix to make example script work better when pasted into a "new script."
// Copyright (c) 2008, Anari Demme
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
// * Neither the names of any contributors nor the names of any organizations
//   affiliated with the contributors may be used to endorse or promote
//   products derived from this software without specific prior written
//   permissions.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// $Id$

// Configuration variables.

// NOTE: This server is only for use for this demonstration.  Do not use this
//       server for any production purposes.  I reserve the right to sabotage
//       your data in entertaining ways if you do. :)
string url              = "http://sloppydemo.appspot.com/";

string demo_key         = "fnord";
float request_timeout   = 15.0; // in my experience these never get close to this number.
float idle_check        = 60.0; // Check for a new message every idle_check seconds.  This is pretty spammy.
integer listen_channel  = 3;

// No real need to touch these.
integer HTTP_REQUEST_NONE = 0;
integer HTTP_REQUEST_GET  = 1;
integer HTTP_REQUEST_PUT  = 2;

integer http_last_request   = HTTP_REQUEST_NONE;
key     http_key            = NULL_KEY;
string  last_message        = "";
integer touched             = FALSE;

get_current_message() {
    if (http_key == NULL_KEY) {
        //llWhisper(0, "Requesting current message from demo server.");
        http_key = llHTTPRequest( url + demo_key, [ ], "");
        http_last_request = HTTP_REQUEST_GET;
        llSetTimerEvent(request_timeout);
    } else {
        llWhisper(0, "There is already a request pending, sorry.");
    }
}

update_text() {
    llSetText(last_message, <1.0,0.8,0.8>, 1.0);
}

send_message(string name, string message) {
    if (http_key == NULL_KEY) {
        //llWhisper(0, "Sending message: " + message);
        http_key = llHTTPRequest(url + demo_key, [ HTTP_METHOD, "PUT" ], name + ": " + message);
        http_last_request = HTTP_REQUEST_PUT;
        llSetTimerEvent(request_timeout);
    } else {
        llWhisper(0, "There is already a request pending, sorry.");
    }
}

reset_state() {
    http_last_request   = HTTP_REQUEST_NONE;
    http_key            = NULL_KEY;
    touched             = FALSE;
}

say_hello() {
    if (http_key == NULL_KEY) {
        last_message        = "";
        llWhisper(0, "Demo client for Sloppy -- Touch to check for a new message at any time.");
        llWhisper(0, "Any message set here will be seen by everyone who is running this script, anywhere in SL.");
        get_current_message();
    }
}

default
{
    state_entry()
    {
        say_hello();
        llListen(listen_channel, "", NULL_KEY, "");
    }

    on_rez(integer param) {
        reset_state();
        say_hello();

    }

    listen( integer channel, string name, key id, string message ) {
        if (channel == listen_channel) {
            send_message(name, message);
        }
    }

    touch_start(integer total_number)
    {
        touched = TRUE;
        get_current_message();
    }

    timer() {
        if (http_key != NULL_KEY) {
            // This is a request timeout.
            llSetTimerEvent(idle_check);
            reset_state();
            llWhisper(0, "Timed out request.");
        } else {
            // This is an idle message update check.
            llSetTimerEvent(0.0);
            get_current_message();

        }
    }

    http_response(key req, integer status, list meta, string content) {
        if (req != http_key) {
            //llWhisper(0, "Unexpected response...");
            return;
        }

        if (http_last_request == HTTP_REQUEST_GET) {
            llSetTimerEvent(idle_check);
            if (status == 200) {
                if (last_message != content) {
                    llWhisper(0, "New message: " + content);
                    llWhisper(0, "You can always set a new one by saying something on /" + (string) listen_channel);
                    last_message = content;
                    update_text();

                } else if (touched) {
                    llWhisper(0, "Sorry, no new message.");
                }

            } else if (status == 404) {
                llWhisper(0, "There is no key by the requested name.");
            } else if (status == 403) {
                llWhisper(0, "Forbidden from retrieving requested key.");
            } else {
                llWhisper(0, "Unexpected HTTP response to GET request: " + (string) status);
            }
            reset_state();
        } else if (http_last_request == HTTP_REQUEST_PUT) {
            llSetTimerEvent(idle_check);
            reset_state();
            get_current_message();

            if (status == 200) {
                llWhisper(0, "Message updated!");
            } else if (status == 201) {
                llWhisper(0, "Created new message!");
            } else if (status == 403) {
                llWhisper(0, "Forbidden from setting requested key.");
            } else {
                llWhisper(0, "Unexpected HTTP response to PUT request: " + (string) status);
            }
        } else {
            reset_state();
            llWhisper(0, "Unexpected HTTP request type.");
        }



    }
}

Anari Demme
ViewVC Help
Powered by ViewVC 1.0.5